//
//      CGE - CRAb Graphics Engine
// 
//      Copyright (C) 2003-2009 by CRAb-UFC
//
//		This file is part of the "CRAb Graphics Engine"
//		See the copyright notice in cge.h for conditions of distribution and use.
//

#ifndef CGE_SERVERSOCKET_CLASS_H__
#define CGE_SERVERSOCKET_CLASS_H__

#include "cgeSocket.h"

namespace basterd
{
    namespace net
    {
        /** 
		 * @brief Encapsulates a ServerSocket, which provides means for implementing a reliable TCP/IP server.
         *
		 * @remarks
		 *
		 *
         */
        class CGE_API ServerSocket
		{

            public:

                /** Default constructor
                 */
                ServerSocket(int maxAllowedConnections = 5);

                /**
                 * Complete constructor
                 *
                 * @param port
                 */
                ServerSocket(unsigned short port, int maxAllowedConnections = 5);

                /**
                 * Complete constructor
                 *
                 * @param host
                 * @param port
                 */
                ServerSocket(const String& host, unsigned short port, int maxAllowedConnections = 5);

                /**
                 *  Destructor.
                 */
                virtual ~ServerSocket(void);
                
                /**
			     * Checks whether this socket is connected or not.
			     */
			    bool isConnected(void) const;

                /**
                 *  accept
                 */
                Socket* accept(void) const;

                /**
                 *  close
                 */
                void close(void) const;
                
                /**
			     * Access the destination port assigned to this server socket.
			     */
                unsigned short getPort(void) const;
                
                /**
			     * 	Access the destination address assigned to this socket
			     */
			    const String& getHostAddress(void) const;

				/**
				* Gets the internal socket id used by this datagram socket.
			 */
				__socket getSocketId(void) const { return serverSocketId; }

            private:

                /**
                 * Copy-constructor: not allowed
                 */
                ServerSocket( const ServerSocket& );

                /**
                 * Assignment operator: not allowed
                 */
                ServerSocket& operator = ( const ServerSocket& );
                
                /**
			     * Holds the address bound to this socket.
			     */
			    String  hostAddress;

                /**
                 * Address bound to this TCP socket.
                 */
                __socketAddress serverAddress;
                /**
                 * Internal socket
                 */
                __socket serverSocketId;

				/**
				 * Maximum allowed connections to this server socket.
				 */
				int maxAllowedConnections;
                
                /**
			     * Holds the port bound to this socket.
			     */
			    unsigned short port;
			    
			    /**
			     * Flag indicating whether this socket is connected or not.
			     */
			    bool connected;

                /**
                 * Internal method responsible for initialize the socket.
                 */
                bool create(void);
                
                /**
			     * Internal method responsible for binding this socket to the given port.
			     *
			     * @param port
			     */
			    bool bind(unsigned short port);
			    
			    /**
			     * Internal method responsible for binding this socket to the given port.
			     *
			     * @param host
			     * @param port
			     */
			    bool bind(const String& host, unsigned short port);
			    
			    /**
                 *  Listen
                 */
                bool listen(void) const;
			    
        }; // end of the ServerSocket class
    }
}
#endif // SERVERSOCKET_H_INCLUDED
